﻿using System;
using BLL;
using DAL;
using Ext.Net;
using Roles;

namespace ERP.Modules
{
    public partial class RecruitmentPlanMain : System.Web.UI.UserControl
    {
        #region[Define]
        private static bool _flag;
        #endregion

        #region[Controller]
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                CheckPermisstion();
                GetPlanMain();
                GetDepartment();
                btnEdit.Disable(true);
                btnDel.Disable(true);
                btnExport.Disable(true);
            }
        }
        //Store
        protected void StoreRecruitment_Plan_Main_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            txtDateBeginFillter.Text = string.Empty;
            txtDateEndFillter.Text = string.Empty;
            txtSearch.Text = string.Empty;
            GetPlanMain();
        }
        protected void StorePlan_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            GetPlan();
        }
        protected void StoreRecruitment_Plan_Main_Detail_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            if (_flag)
            {
                GetPlanMainTemp();
            }
            else
            {
                GetPlanMainEdit(int.Parse(txtID_Main.Text));
            }
        }
        //Button
        protected void btnAddNew_Click(object sender, DirectEventArgs e)
        {
            _flag = true;
            DelValueDetailTemp();
            ClearText();
            ClearTextDetail();
            WindowsOrderRecruitment_Plan_Main.Show();
        }
        protected void btnEdit_Click(object sender, DirectEventArgs e)
        {
            var sm = grRecruitment_Plan_Main.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (var row in sm.SelectedRows)
                {
                    _flag = false;
                    ClearText();
                    GetPlanMainEdit(int.Parse(row.RecordID));
                    WindowsOrderRecruitment_Plan_Main.Show();
                }
        }
        protected void btnDel_Click(object sender, DirectEventArgs e)
        {
            var sm = grRecruitment_Plan_Main.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (var row in sm.SelectedRows)
                {
                    try
                    {
                        var obj = new EtRecruitmentPlanMain { ID_Main = int.Parse(row.RecordID) };
                        BllRecruitmentPlanMainMain.Delete(obj);
                    }
                    catch (Exception ex)
                    {
                        HTML.clsFormat.Alert_ERROR("Thông Báo", ex.Message);
                    }
                }
            GetPlanMain();
        }
        protected void MenuItemExportExcel_Click(object sender, DirectEventArgs e)
        {

        }
        protected void btnFillter_Click(object sender, DirectEventArgs e)
        {
            GetPlanMainFillter();
        }
        protected void btnSavePopUp_Click(object sender, DirectEventArgs e)
        {
            Insert();
        }
        protected void btnAddMaterial_Click(object sender, DirectEventArgs e)
        {
            var sm = grPlan.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null) sm.DeselectAll();
            GetPlan();
            WindowPlan.Show();
        }
        protected void btnDelMaterial_Click(object sender, DirectEventArgs e)
        {
            if (_flag)
            {
                InsertValueDetail();
            }
            else
            {
                if (txtID_Main_Detail.Text == string.Empty)
                {
                    UpdateValueDetail();
                }
                else
                {
                    UpdateDetail();
                }
            }
        }
        protected void btnCancelPopUp_Click(object sender, DirectEventArgs e)
        {
            WindowsOrderRecruitment_Plan_Main.Close();
        }
        protected void btnRefreshRecruitment_Plan_MainCode_Click(object sender, DirectEventArgs e)
        {
            txtOrderRecruitment_Plan_MainCode.Text = AutoCode();
        }
        protected void btnSelectPlan_Click(object sender, DirectEventArgs e)
        {
            var _html = string.Empty;
            var sm = grPlan.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (var row in sm.SelectedRows)
                {
                    _html += row.RecordID + ",";
                    txtPlan_Name.Text = _html.Remove(_html.Length - 1);
                }
            WindowPlan.Close();
        }
        protected void btnDelPlan_Click(object sender, DirectEventArgs e)
        {
            var sm = grRecruitment_Plan_Main_Detail.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (var row in sm.SelectedRows)
                {
                    try
                    {
                        var obj = new EtRecruitmentPlanMainDetail { ID_Main_Detail = int.Parse(row.RecordID) };
                        BllRecruitmentPlanMainDetail.Delete(obj);
                    }
                    catch (Exception ex)
                    {
                        HTML.clsFormat.Alert_ERROR("Thông Báo", ex.Message);
                    }
                }
            if (_flag)
            {
                GetPlanMainTemp();
            }
            else
            {
                GetPlanMainEdit(int.Parse(txtID_Main.Text));
            }
        }
        protected void btnReloadCode_Click(object sender, DirectEventArgs e)
        {
            SelectBoxDepartment.Text = string.Empty;
            GetDepartment();
        }
        //Double-Click
        protected void Recruitment_Plan_MainDblClick(object sender, DirectEventArgs e)
        {
            _flag = false;
            int ID_Main = int.Parse(e.ExtraParams["ID_Main"]);
            GetPlanMainEdit(ID_Main);
            WindowsOrderRecruitment_Plan_Main.Show();
        }
        protected void grRecruitmentDetail_MainDblClick(object sender, DirectEventArgs e)
        {
            int ID_Main_Detail = int.Parse(e.ExtraParams["ID_Main_Detail"]);
            GetPlanMainDetailEdit(ID_Main_Detail);
        }
        //Search
        protected void Search_Event(object sender, DirectEventArgs e)
        {
            var _clsRecruitmentPlanMainSearch = new BllRecruitmentPlanMainMain();
            var dtSearch = _clsRecruitmentPlanMainSearch.GetRecruitmentPlanMain(txtSearch.Text);
            if (dtSearch != null && dtSearch.Rows.Count > 0)
            {
                StoreRecruitment_Plan_Main.DataSource = dtSearch;
                StoreRecruitment_Plan_Main.DataBind();
            }
            else
            {
                grRecruitment_Plan_Main.GetStore().RemoveAll();
            }
        }
        #endregion

        #region[Method]
        private void GetDepartment()
        {
            var _clsGetDepartment = new Department_BLL();
            var _dtGetDepartment = _clsGetDepartment.BindGirdtb_Department(string.Empty);
            if (_dtGetDepartment != null && _dtGetDepartment.Rows.Count > 0)
            {
                StoreDepartment.DataSource = _dtGetDepartment;
                StoreDepartment.DataBind();
            }
            else
            {
                SelectBoxDepartment.GetStore().RemoveAll();
            }
        }
        private void GetPlan()
        {
            var _clsGetPlan = new BllRecruitmentPlan();
            var _dtGetPlan = _clsGetPlan.GetRecruitmentPlan(string.Empty);
            if (_dtGetPlan != null && _dtGetPlan.Rows.Count > 0)
            {
                StorePlan.DataSource = _dtGetPlan;
                StorePlan.DataBind();
            }
            else
            {
                grPlan.GetStore().RemoveAll();
            }
        }
        private void GetPlanMain()
        {
            var _clsGetPlanMain = new BllRecruitmentPlanMainMain();
            var _dtGetPlanMain = _clsGetPlanMain.GetRecruitmentPlanMain(string.Empty);
            if (_dtGetPlanMain != null && _dtGetPlanMain.Rows.Count > 0)
            {
                StoreRecruitment_Plan_Main.DataSource = _dtGetPlanMain;
                StoreRecruitment_Plan_Main.DataBind();
            }
            else
            {
                grRecruitment_Plan_Main.GetStore().RemoveAll();
            }
        }
        private void GetPlanMainFillter()
        {
            var _clsGetPlanMainFillter = new BllRecruitmentPlanMainMain();
            if (txtDateBeginFillter.Text == string.Empty && txtDateEndFillter.Text == string.Empty)
            {
                var _dtGetPlanMainFillter = _clsGetPlanMainFillter.GetRecruitmentPlanMain(string.Empty);
                if (_dtGetPlanMainFillter != null && _dtGetPlanMainFillter.Rows.Count > 0)
                {
                    StoreRecruitment_Plan_Main.DataSource = _dtGetPlanMainFillter;
                    StoreRecruitment_Plan_Main.DataBind();
                }
                else
                {
                    grRecruitment_Plan_Main.GetStore().RemoveAll();
                }
            }
            else
            {
                var _dtGetPlanMainFillter = _clsGetPlanMainFillter.GetRecruitmentPlanMainFillter(txtDateBeginFillter.Text,
                txtDateEndFillter.Text);
                if (_dtGetPlanMainFillter != null && _dtGetPlanMainFillter.Rows.Count > 0)
                {
                    StoreRecruitment_Plan_Main.DataSource = _dtGetPlanMainFillter;
                    StoreRecruitment_Plan_Main.DataBind();
                }
                else
                {
                    grRecruitment_Plan_Main.GetStore().RemoveAll();
                }
            }
            
        }
        private void GetPlanMainEdit(int id)
        {
            var _clsGetPlanMainEdit = new BllRecruitmentPlanMainMain();
            var _clsGetPlanMainDetail = new BllRecruitmentPlanMainDetail();
            var _dtGetPlanMainEdit = _clsGetPlanMainEdit.GetRecruitmentPlanMainEdit(id);
            var _dtGetPlanMainDetail = _clsGetPlanMainDetail.GetRecruitmentPlanMainDetail(id);
            if (_dtGetPlanMainEdit != null && _dtGetPlanMainEdit.Rows.Count > 0)
            {
                txtID_Main.Text = _dtGetPlanMainEdit.Rows[0]["ID_Main"].ToString();
                txtOrderRecruitment_Plan_MainCode.Text = _dtGetPlanMainEdit.Rows[0]["Main_Code"].ToString();
                txtMain_Name.Text = _dtGetPlanMainEdit.Rows[0]["Main_Name"].ToString();
                SelectBoxDepartment.SetValue(int.Parse(_dtGetPlanMainEdit.Rows[0]["Department_Id"].ToString()));
                txtDateBegin.Text = _dtGetPlanMainEdit.Rows[0]["Datebegin"].ToString();
                txtDateEnd.Text = _dtGetPlanMainEdit.Rows[0]["DateEnd"].ToString();
                txtQuarters.Text = _dtGetPlanMainEdit.Rows[0]["Quarters"].ToString();
                txtYears.Text = _dtGetPlanMainEdit.Rows[0]["Years"].ToString();
                if (_dtGetPlanMainDetail != null && _dtGetPlanMainDetail.Rows.Count > 0)
                {
                    StoreRecruitment_Plan_Main_Detail.DataSource = _dtGetPlanMainDetail;
                    StoreRecruitment_Plan_Main_Detail.DataBind();
                }
                else
                {
                    grRecruitment_Plan_Main_Detail.GetStore().RemoveAll();
                }
            }
        }
        private void GetPlanMainDetailEdit(int id)
        {
            var _clsGetPlanMainDetail = new BllRecruitmentPlanMainDetail();
            var _dtGetPlanMainEdit = _clsGetPlanMainDetail.GetRecruitmentPlanMainDetailEdit(id);
            if (_dtGetPlanMainEdit != null && _dtGetPlanMainEdit.Rows.Count > 0)
            {
                txtID_Main_Detail.Text = _dtGetPlanMainEdit.Rows[0]["ID_Main_Detail"].ToString();
                txtLevel_Main.Text = _dtGetPlanMainEdit.Rows[0]["Level_Main"].ToString();
                txtOffice.Text = _dtGetPlanMainEdit.Rows[0]["Office"].ToString();
                txtTimeLine.Text = _dtGetPlanMainEdit.Rows[0]["TimeLine"].ToString();
                txtPlan_Name.Text = _dtGetPlanMainEdit.Rows[0]["Plan_Name"].ToString();
                txtQuanlity.Text = _dtGetPlanMainEdit.Rows[0]["Quanlity"].ToString();
            }
        }
        private void GetPlanMainTemp()
        {
            var _clsGetPlanMainDetail = new BllRecruitmentPlanMainDetail();
            var _dtGetPlanMainDetailTemp =
                _clsGetPlanMainDetail.GetRecruitmentPlanMainDetailTemp(int.Parse(Session["Users_Id"].ToString()));
            if (_dtGetPlanMainDetailTemp != null && _dtGetPlanMainDetailTemp.Rows.Count > 0)
            {
                StoreRecruitment_Plan_Main_Detail.DataSource = _dtGetPlanMainDetailTemp;
                StoreRecruitment_Plan_Main_Detail.DataBind();
            }
            else
            {
                grRecruitment_Plan_Main_Detail.GetStore().RemoveAll();
            }
        }
        private void Insert()
        {
            if (_flag)
            {
                InsertValue();
            }
            else
            {
                if (CheckPermisstionDobclick())
                {
                    HTML.clsFormat.Alert_ERROR("Thông Báo", "Bạn không được cấp quyền để sửa đổi !");
                }
                else
                {
                    UpdateValue();
                }
            }
        }
        private void InsertValue()
        {
            try
            {
                var obj = new EtRecruitmentPlanMain
                {
                    Main_Code = txtOrderRecruitment_Plan_MainCode.Text,
                    Main_Name = txtMain_Name.Text,
                    Datebegin = DateTime.Parse(txtDateBegin.Text),
                    DateEnd = DateTime.Parse(txtDateEnd.Text),
                    Quarters = int.Parse(txtQuarters.Text),
                    Years = int.Parse(txtYears.Text),
                    Users_Id = int.Parse(Session["Users_Id"].ToString()),
                    Department_Id = int.Parse(SelectBoxDepartment.SelectedItem.Value)
                };
                BllRecruitmentPlanMainMain.Insert(obj);
                HTML.clsFormat.Alert_INFO("Thông Báo", "Thêm mới thành công !");
                GetPlanMain();
                ClearText();
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        private void UpdateValue()
        {
            try
            {
                var obj = new EtRecruitmentPlanMain
                {
                    ID_Main = int.Parse(txtID_Main.Text),
                    Main_Code = txtOrderRecruitment_Plan_MainCode.Text,
                    Main_Name = txtMain_Name.Text,
                    Datebegin = DateTime.Parse(txtDateBegin.Text),
                    DateEnd = DateTime.Parse(txtDateEnd.Text),
                    Quarters = int.Parse(txtQuarters.Text),
                    Years = int.Parse(txtYears.Text),
                    Users_Id = int.Parse(Session["Users_Id"].ToString()),
                    Department_Id = int.Parse(SelectBoxDepartment.SelectedItem.Value)
                };
                BllRecruitmentPlanMainMain.Update(obj);
                HTML.clsFormat.Alert_INFO("Thông Báo", "Cập nhật thành công !");
                GetPlanMain();
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        private void InsertValueDetail()
        {
            try
            {
                var obj = new EtRecruitmentPlanMainDetail
                {
                    Level_Main = txtLevel_Main.Text,
                    ID_Main = null,
                    Office = txtOffice.Text,
                    Quanlity = int.Parse(txtQuanlity.Text),
                    TimeLine = txtTimeLine.Text,
                    Plan_Name = txtPlan_Name.Text,
                    Users_Id = int.Parse(Session["Users_Id"].ToString()),
                    Flag = 1
                };
                BllRecruitmentPlanMainDetail.Insert(obj);
                HTML.clsFormat.Alert_INFO("Thông Báo", "Thêm mới thành công !");
                ClearTextDetail();
                GetPlanMainTemp();
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        private void UpdateValueDetail()
        {
            try
            {
                var obj = new EtRecruitmentPlanMainDetail
                {
                    Level_Main = txtLevel_Main.Text,
                    ID_Main = int.Parse(txtID_Main.Text),
                    Office = txtOffice.Text,
                    Quanlity = int.Parse(txtQuanlity.Text),
                    TimeLine = txtTimeLine.Text,
                    Plan_Name = txtPlan_Name.Text,
                    Users_Id = int.Parse(Session["Users_Id"].ToString()),
                    Flag = 0
                };
                BllRecruitmentPlanMainDetail.Insert(obj);
                HTML.clsFormat.Alert_INFO("Thông Báo", "Cập nhật mới thành công !");
                ClearTextDetail();
                GetPlanMainEdit(int.Parse(txtID_Main.Text));
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        private void UpdateDetail()
        {
            try
            {
                var obj = new EtRecruitmentPlanMainDetail
                {
                    ID_Main_Detail = int.Parse(txtID_Main_Detail.Text),
                    ID_Main = int.Parse(txtID_Main.Text),
                    Level_Main = txtLevel_Main.Text,
                    Office = txtOffice.Text,
                    Quanlity = int.Parse(txtQuanlity.Text),
                    TimeLine = txtTimeLine.Text,
                    Plan_Name = txtPlan_Name.Text,
                    Users_Id = int.Parse(Session["Users_Id"].ToString()),
                    Flag = 0
                };
                BllRecruitmentPlanMainDetail.Update(obj);
                HTML.clsFormat.Alert_INFO("Thông Báo", "Cập nhật thành công !");
                GetPlanMainEdit(int.Parse(txtID_Main.Text));
                ClearTextDetail();
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        private void DelValueDetailTemp()
        {
            try
            {
                var obj = new EtRecruitmentPlanMainDetail {Users_Id = int.Parse(Session["Users_Id"].ToString())};
                BllRecruitmentPlanMainDetail.DeleteTemp(obj);
                GetPlanMainTemp();
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        #endregion

        #region[ClearText]
        private void ClearText()
        {
            FormPanelOrderRecruitment_Plan_Main.Reset();
            FormPanelDetail.Reset();
            txtOrderRecruitment_Plan_MainCode.Text = AutoCode();
            txtDateBegin.Text = DateTime.Now.ToString();
            txtDateEnd.Text = DateTime.Now.ToString();
            txtID_Main.Text = string.Empty;
            grRecruitment_Plan_Main_Detail.GetStore().RemoveAll();
        }
        private void ClearTextDetail()
        {
            FormPanelDetail.Reset();
            txtDateBegin.Text = DateTime.Now.ToString();
            txtDateEnd.Text = DateTime.Now.ToString();
        }
        #endregion

        #region[AutoCode]
        private string AutoCode()
        {
            int i;
            var _clsGetRecruitmentPlanMain = new BllRecruitmentPlanMainMain();
            var dt = _clsGetRecruitmentPlanMain.GetRecruitmentPlanMain(string.Empty);
            if (dt != null && dt.Rows.Count > 0)
            {
                int max = int.Parse(dt.Rows[0]["ID_Main"].ToString());
                for (i = 0; i < dt.Rows.Count; i++)
                {
                    if (int.Parse(dt.Rows[i]["ID_Main"].ToString()) > max)
                    {
                        max = int.Parse(dt.Rows[i]["ID_Main"].ToString());
                    }
                }
                i = max + 1;
            }
            else
            {
                i = 0;
            }
            return "MAKH00" + i;
        }
        #endregion

        #region[Permisstion]
        private void CheckPermisstion()
        {
            if (Session["login"] != null && Session["USer_Detail_Id"].ToString() != string.Empty)
            {
                bool Is_BtnAdd;
                bool Is_BtnEdit;
                bool Is_BtnView;
                bool Is_BtnDel;
                bool Is_BtnPrint;
                bool Is_BtnCancel;
                bool Is_BtnReview;

                SecurityServices.HasPermisstion_Btn(StringContent.RecruitmentPlanMain, out Is_BtnAdd, out Is_BtnEdit, out Is_BtnDel, out Is_BtnView, out Is_BtnCancel, out Is_BtnPrint, out Is_BtnReview);
                if (Is_BtnAdd == false)
                {
                    btnAddNew.Visible = false;
                }
                if (Is_BtnEdit == false)
                {
                    btnEdit.Visible = false;
                }
                if (Is_BtnView == false)
                {
                    PanelRecruitment_Plan_Main.Visible = false;
                }
                if (Is_BtnDel == false)
                {
                    btnDel.Visible = false;
                }
                if (Is_BtnPrint == false)
                {
                    btnExport.Visible = false;
                }
            }
        }
        private bool CheckPermisstionDobclick()
        {
            bool check = false;
            if (Session["login"] != null && Session["USer_Detail_Id"].ToString() != string.Empty)
            {
                bool Is_BtnAdd;
                bool Is_BtnEdit;
                bool Is_BtnView;
                bool Is_BtnDel;
                bool Is_BtnPrint;
                bool Is_BtnCancel;
                bool Is_BtnReview;

                SecurityServices.HasPermisstion_Btn(StringContent.RecruitmentPlanMain, out Is_BtnAdd, out Is_BtnEdit, out Is_BtnDel, out Is_BtnView, out Is_BtnCancel, out Is_BtnPrint, out Is_BtnReview);
                if (Is_BtnEdit == false)
                {
                    check = true;
                }
            }
            return check;
        }
        #endregion
    }
}